//
//  QLNiceLoginViewController.m
//  Nice
//
//  Created by huangyueqi on 2017/7/26.
//  Copyright © 2017年 sjyt. All rights reserved.
//

#import "QLNiceLoginViewController.h"
#import "QLNiceChatViewController.h"
#import "QLNiceNetworkTools.h"

@interface QLNiceLoginViewController () <UITextFieldDelegate>

@property (weak, nonatomic) IBOutlet UITextField *userNameTextField;
@property (weak, nonatomic) IBOutlet UITextField *passwordTextField;
@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@end

@implementation QLNiceLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
}

#pragma mark - SetupUI
- (void)setupUI {
    
    self.userNameTextField.delegate = self;
    self.passwordTextField.delegate = self;
    
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillHide:)
                                                 name:UIKeyboardWillHideNotification
                                               object:nil];
}

#pragma mark - UITextFieldDelegate
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    
    [self.userNameTextField resignFirstResponder];
    [self.passwordTextField resignFirstResponder];
    return YES;
}

#pragma mark - Action
//获取键盘高度计算偏移量
- (void)keyboardWillShow:(NSNotification *)notification {
    
    //获取键盘的值
    CGRect keyboardRect = [[[notification userInfo] objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
    //最后获取高度 宽度也是同理可以获取
    CGFloat keyboardHeight = keyboardRect.size.height;

    CGFloat offset = keyboardHeight + CGRectGetMaxY(self.passwordTextField.frame) - SCREEN_HEIGHT + 5;

    if (offset < 0) {
        offset = 0;
    }
    [self.scrollView setContentOffset:CGPointMake(0, offset) animated:YES];
}

- (void)keyboardWillHide:(NSNotification *)notification {
    
    [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
}

- (IBAction)loginButton:(id)sender {
    
    QLNiceChatViewController *vc = [[UIStoryboard storyboardWithName:@"QLNice" bundle:nil] instantiateViewControllerWithIdentifier:@"QLNiceChatViewController"];
    [self presentViewController:vc animated:YES completion:nil];
}

- (void)dealloc {
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}


@end
